package com.starsea.book.ui.newpage.activity

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityCoinExchangeBinding
import com.starsea.book.ui.newpage.LoginActivity
import com.starsea.book.ui.newpage.adapter.CoinExchangeAdapter
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-03-28 18 GMT+8
 * @desc :
 */
class CoinExchangeActivity : VMBaseActivity<ActivityCoinExchangeBinding, WalletViewModel>() {
    override val binding by viewBinding(ActivityCoinExchangeBinding::inflate)
    override val viewModel by viewModels<WalletViewModel>()

    lateinit var mAdapter: CoinExchangeAdapter

    @SuppressLint("SetTextI18n")
    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        viewModel.getWalletInfo()

        viewModel.walletInfoResult.observe(this) { data ->
            if (data.status == 200) {

                binding.tvNumber.text =
                    if (TextUtils.isEmpty(data.data!!.goldBalance)) "0" else {
                        val goldBalance = data.data!!.goldBalance!!.toDouble()
                        if (goldBalance > 10000) {
                            "${(goldBalance / 10000.0).finialDouble()}万"
                        } else {
                            goldBalance.finialDouble()
                        }
                    }
                binding.tvNumberDesc.text =
                    "（约${
                        if (TextUtils.isEmpty(data.data!!.goldBalance)) "0" else {
                            val goldBalance = data.data!!.goldBalance!!.toDouble()
                            val ratio = data.data!!.subRatio!!.toDouble()
                            (goldBalance / ratio).finialDouble()
                        }
                    }元）"

                mAdapter = CoinExchangeAdapter(this@CoinExchangeActivity, data.data!!.subRatio!!) {
                    val builder: AlertDialog.Builder =
                        AlertDialog.Builder(this@CoinExchangeActivity)
                    builder.setTitle("温馨提示")
                    builder.setMessage(
                        "是否花费${
                            if (TextUtils.isEmpty(it.amount)) "0" else {
                                val goldBalance = it.amount!!.toDouble()
                                val ratio = data.data!!.subRatio!!.toDouble()
                                (goldBalance * ratio).finialDouble()
                            }
                        }金币兑换"
                    )
                    builder.setPositiveButton("确定",
                        DialogInterface.OnClickListener { dialog, which -> //模拟位置
                            viewModel.goldExchange(it.ruleId ?: "")
                        })
                    builder.setNegativeButton(
                        "取消"
                    ) { dialog, which ->

                    }
                    builder.show()
                }

                binding.rvCash.layoutManager = GridLayoutManager(this, 2)
                binding.rvCash.adapter = mAdapter

                viewModel.getCoinExchangeList()

            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }


        viewModel.coinExchangeListResult.observe(this) { data ->
            if (data.status == 200) {
                mAdapter.setItems(data.data)
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }


        viewModel.coinExchangeResultResult.observe(this) { data ->
            if (data.status == 200) {
                toastOnUi("兑换成功")
                viewModel.getWalletInfo()
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }


    }
}